/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package neo.workforce.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import neo.workforce.dao.Activity;
import neo.workforce.dao.ProcessDAO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author krisada
 */
@Controller
public class ProcessController {

    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private ProcessDAO processDao = null;

    public ProcessController() {
    }

    public ProcessDAO getProcessDao() {
        return processDao;
    }

    public void setProcessDao(ProcessDAO processDao) {
        this.processDao = processDao;
    }

    @RequestMapping(value = "/processInit", method = RequestMethod.GET)
    public ModelAndView processInit() {
        List<neo.workforce.dao.Process> processes = this.processDao.findByName(0, 10, null);
        log.info("return size:" + processes.size());
        return new ModelAndView("searchProcess", "processes", processes);
    }

    @RequestMapping(value = "/processEntry", method = RequestMethod.GET)
    public ModelAndView processEntry(ModelMap model) {
        neo.workforce.dao.Process process = new neo.workforce.dao.Process();
        System.out.println("Activity size:" + process.getActivities().size());
        model.addAttribute("mode_action", "addProcess.htm");
        //model.addAttribute("act_size", Integer.toString(process.getActivities().size()));
        //model.addAttribute("activities", process.getActivities());
        ModelAndView mvc = new ModelAndView("process_entry");
        mvc.addObject("command", process);
        //mvc.addObject("activities", process.getActivities());
        mvc.addObject("act_size", Integer.toString(process.getActivities().size()));
        //log.info("Activity size:" + process.getActivities().size());
        return mvc;
    }

    @RequestMapping(value = "/addProcess", method = RequestMethod.POST)
    public String addProcess(@ModelAttribute("SpringWeb") neo.workforce.dao.Process process,
            ModelMap model) {
        process.setCreateDate(new java.util.Date(Calendar.getInstance().getTimeInMillis()));
        /*Activity activity = new Activity();
        activity.setActivityId("ACT0002");
        activity.setName("ACT leave 2");
        activity.setDescription("ACT leave 2");
        activity.setCreateDate(Calendar.getInstance().getTime());
        activity.setProcess(process);
        List ls = new ArrayList();
        ls.add(activity);
        process.setActivities(ls);*/
        Long id = processDao.save(process);
        model.addAttribute("id", id.longValue());
        model.addAttribute("processId", process.getProcessId());
        model.addAttribute("name", process.getName());
        model.addAttribute("description", process.getDescription());
        return "result";
    }

    @RequestMapping(value = "/findProcess", method = RequestMethod.GET)
    public ModelAndView findProcess(@RequestParam("id") String pid,
            ModelMap model) {
        neo.workforce.dao.Process process = processDao.find(Long.parseLong(pid));
        model.addAttribute("id", process.getId().longValue());
        model.addAttribute("processId", process.getProcessId());
        model.addAttribute("name", process.getName());
        model.addAttribute("description", process.getDescription());
        model.addAttribute("mode_action", "editProcess.htm");
        //model.addAttribute("activities", process.getActivities());
        ModelAndView mvc = new ModelAndView("process_entry");
        mvc.addObject("command", process);
        model.addAttribute("act_size", Integer.toString(process.getActivities().size()));
        mvc.addObject("activities", process.getActivities());
        log.info("Activity size:"+ process.getActivities().size());
        //return new ModelAndView("process_entry", "command", process);
        return mvc;
    }

    @RequestMapping(value = "/editProcess", method = RequestMethod.POST)
    public String editProcess(@ModelAttribute("SpringWeb") neo.workforce.dao.Process process,
            ModelMap model) {
        boolean chk = processDao.update(process);
        model.addAttribute("id", process.getId().longValue());
        model.addAttribute("processId", process.getProcessId());
        model.addAttribute("name", process.getName());
        model.addAttribute("description", process.getDescription());
        return "result";
    }

    @RequestMapping(value = "/processJsons", method = RequestMethod.GET)
    public @ResponseBody
    List<neo.workforce.dao.Process> getProcessJSON() {
        List<neo.workforce.dao.Process> processes = this.processDao.findByName(0, 5, null);
        log.info("return size:" + processes.size());
        return processes;

    }
}